package commons.ui.editor;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import commons.resources.CommonsPluginResources;
import commons.ui.listener.DirtyEvent;
import commons.ui.listener.DirtyManager;
import commons.ui.listener.IDirtyListener;

/**
 * @author Ihor Strutynskyj
 */
public abstract class StatusEditorPart extends EditorPart implements IRefreshableWorkbenchPart, IStatusEditor {
    private IStatusLineManager _statusLine;
    private Image _errorImage, _warningImage, _infoImage;
    protected DirtyManager _dirtyManager = new DirtyManager(this);
    private boolean _disposed;

    @Override
    public void init(IEditorSite site, IEditorInput input) throws PartInitException {
	setSite(site);
	setInput(input);
	if (input != null) {
	    setPartName(input.getName());
	}
	_statusLine = site.getActionBars().getStatusLineManager();
	_errorImage = CommonsPluginResources.getImage("error_obj.gif");
	_warningImage = CommonsPluginResources.getImage("warning_obj.gif");
	_infoImage = CommonsPluginResources.getImage("info_obj.gif");
	setSaveOnDirty();
    }

    protected void setSaveOnDirty() {
	_dirtyManager.addDirtyListener(new IDirtyListener() {
	    public void dirtyStateChanged(DirtyEvent event) {
		// notify listeners that dirty property has changed
		firePropertyChange(PROP_DIRTY);
	    }
	});
    }

    public void init(IEditorInput input) {
    }

    protected IPropertyListener getPropertyListener() {
	return null;
    }

    /**
     * @see org.eclipse.ui.IEditorPart#isDirty()
     */
    @Override
    public boolean isDirty() {
	return _dirtyManager.isDirty();
    }

    public boolean setDirty(boolean v) {
	return _dirtyManager.setDirty(v);
    }

    /**
     * @see org.eclipse.ui.IEditorPart#doSave()
     */
    @Override
    public void doSave(IProgressMonitor monitor) {
    }

    /**
     * @see org.eclipse.ui.IEditorPart#doSaveAs()
     */
    @Override
    public void doSaveAs() {
    }

    /**
     * @see org.eclipse.ui.IEditorPart#gotoMarker(IMarker)
     */
    public void gotoMarker(IMarker marker) {
    }

    /**
     * @see org.eclipse.ui.IEditorPart#isSaveAsAllowed()
     */
    @Override
    public boolean isSaveAsAllowed() {
	return false;
    }

    public void showStatusMessage(String message) {
	if (_statusLine != null) {
	    _statusLine.setMessage(message);
	}
    }

    public void showStatusInfoMessage(String message) {
	if (_statusLine != null) {
	    _statusLine.setMessage(_infoImage, message);
	}
    }

    public void showStatusErrorMessage(String message) {
	if (_statusLine != null) {
	    _statusLine.setErrorMessage(_errorImage, message);
	}
    }

    public void showStatusWarningMessage(String message) {
	if (_statusLine != null) {
	    _statusLine.setMessage(_warningImage, message);
	}
    }

    public void clearStatusMessage() {
	if (_statusLine != null) {
	    _statusLine.setMessage(null);
	}
    }

    public void clearStatusErrorMessage() {
	if (_statusLine != null) {
	    _statusLine.setErrorMessage(null);
	}
    }

    public void clearStatusLine() {
	if (_statusLine != null) {
	    _statusLine.setMessage(null);
	    _statusLine.setErrorMessage(null);
	}
    }

    @Override
    public void createPartControl(Composite parent) {
    }

    @Override
    public void setFocus() {
    }

    public boolean canRefresh() {
	return _dirtyManager.isDirty();
    }

    @Override
    public void setPartName(String name) {
	super.setPartName(name);
    }

    public void refresh() {
    }

    @Override
    public void dispose() {
	setDisposed(true);
	clearStatusLine();
	_statusLine = null;
	_dirtyManager = null;
	_errorImage = _warningImage = _infoImage = null;
	SimpleEditorInput input = ((SimpleEditorInput) getEditorInput());
	if (input != null) {
	    input.dispose();
	}
	super.dispose();
    }

    public IAction setAction(String actionId, IToolBarManager manager) {
	IAction action = null;
	IContributionItem item = manager.find(actionId);
	if (item instanceof ActionContributionItem) {
	    ActionContributionItem toolbarActionItem = ((ActionContributionItem) item);
	    action = toolbarActionItem.getAction();
	    // toolbarActionItem.setMode(ActionContributionItem.MODE_FORCE_TEXT);
	}
	return action;
    }

    public DirtyManager getDirtyManager() {
	return _dirtyManager;
    }

    public boolean isDisposed() {
	return _disposed;
    }

    public void setDisposed(boolean disposed) {
	_disposed = disposed;
    }
}
